package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：笔友会信息页面的文章/帖子列表
 */
public class PenpalArticleItemAdapter extends RecyclerView.Adapter<PenpalArticleItemAdapter.ViewHolder> {
    private Context context;
    private List<PenpalArticleResponse.ResponseBean> list;
    private AdapterListener listener;

    public PenpalArticleItemAdapter(Context context, List<PenpalArticleResponse.ResponseBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_penpal_article_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.name.setText(list.get(i).getBi_name());
        viewHolder.time.setText(DateUtils.ConvertDate(list.get(i).getCreate_datetime()));
        viewHolder.title.setText(list.get(i).getTitle());
        viewHolder.articleContent.setText(list.get(i).getContents_header());
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(),
                R.drawable.user_head_icon, R.drawable.user_head_icon);
        viewHolder.vip.setVisibility(list.get(i).getVip() ? View.VISIBLE : View.GONE);
        if (list.get(i).getIstop() == 0) {
            viewHolder.bottomLabel.setVisibility(View.GONE);
        } else {
            viewHolder.bottomLabel.setVisibility(View.VISIBLE);
            viewHolder.bottomLabel.setBackgroundResource(R.drawable.top_icon);
        }
        if (TextUtils.equals(list.get(i).getTopic_type(), "transpond")) {
            viewHolder.shareFlag.setVisibility(View.VISIBLE);
        } else {
            viewHolder.shareFlag.setVisibility(View.GONE);
        }
        if (list.get(i).getRank() == 1) {
            viewHolder.level.setText("会长");
            viewHolder.level.setVisibility(View.VISIBLE);
        } else {
            viewHolder.level.setVisibility(View.GONE);
        }
        viewHolder.comment.setText("评论 " + list.get(i).getCommentCount());
        viewHolder.praise.setText("点赞 " + list.get(i).getLikeCount());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.callBack(list.get(i));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title, name, time, comment, praise, level, articleContent;
        private ImageView logo, bottomLabel, vip, shareFlag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            level = itemView.findViewById(R.id.author_level);
            title = itemView.findViewById(R.id.article_name);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            time = itemView.findViewById(R.id.wirte_time);
            bottomLabel = itemView.findViewById(R.id.bottom_label);
            comment = itemView.findViewById(R.id.bottom_comment);
            praise = itemView.findViewById(R.id.bottom_praise);
            vip = itemView.findViewById(R.id.vip);
            shareFlag = itemView.findViewById(R.id.share_flag);
            articleContent = itemView.findViewById(R.id.article_content);
        }
    }
}
